<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Peminjaman extends CI_Controller {

		function __construct(){
			parent::__construct();
			$this->load->model('Peminjaman_model');
			$this->load->model('Anggota_model');
			$this->load->helper('all_helper');
			$this->load->helper('date');
			if(!$this->session->userdata('rule')== '1' && !$this->session->userdata('username') ){
			redirect('kategori');
		}
		}
		
		function index(){
			$offset = $this->uri->segment(3);
		$config['base_url'] = $this->config->site_url().'peminjaman/index';
		$config['total_rows'] = $this->db->count_all('tb_peminjaman');
		$config['per_page'] = 10;
		$config['uri_segment'] = '3';
		
		

		
			$data['urutan'] = $this->uri->segment(3);
			$data['title']		= 'Data Peminjaman';
			$data['status'] = 'Data Peminjaman';
			$data['main_view']	= 'peminjaman/pinjam';
			$data['query']		= $this->Peminjaman_model->getPeminjaman('list',FALSE,$config['per_page'],$offset);
			$this->pagination->initialize($config);
			$this->load->view('index',$data);
		
		
		}
				
		function cari(){
		$offset = $this->uri->segment(3);
		$config['base_url'] = $this->config->site_url().'cari/index';
		$config['total_rows'] = $this->db->count_all('tb_peminjaman');
		$config['per_page'] = 10;
		$config['uri_segment'] = '3';
			$data['urutan'] = $this->uri->segment(3);
			$data['title'] = 'Tambah Data Peminjaman';
			$data['main_view'] = 'peminjaman/add';
			$data['urutan'] = $this->uri->segment(3);
			$cari = $this->input->post('search');
			$data['query'] =$this->Anggota_model->getAnggota('cari',FALSE,$cari,FALSE,$config['per_page'],$offset);
			$this->pagination->initialize($config);
			$this->load->view('index',$data);
		
		}
		
		function add(){
			$data['title']		= 'Tambah Data Peminjaman';
			$data['main_view']	= 'peminjaman/add';
			$data['status'] = 'Add Data Peminjaman';
			$data['urutan']		= $this->uri->segment(3);
			$data['query']		= $this->Anggota_model->getAnggota('list',FALSE,FALSE,FALSE,FALSE);
			$this->load->view('index',$data);
				
		
		}
		
		function tambah(){
			$this->form_validation->set_rules('tgl','tanggal', 'required');
			if($this->form_validation->run() == FALSE){
			$data['title']		= 'Tambah Data Peminjaman';
			$id					= $this->uri->segment(3);
			$data['id_member']	=$id;
					$datestring = "%Y-%m-%d";
			$data['tgl']= mdate($datestring);
			$id_member	= $this->uri->segment(3);
	
			$data['main_view']	= 'peminjaman/tambah';
			$data['status'] = 'Tambah Data Peminjaman';
			$data['row']		= $this->Anggota_model->getAnggota('by_id',$id_member,FALSE,FALSE);
	
			
			$data['query']		= $this->Peminjaman_model->getPeminjaman('temp',$id,FALSE,FALSE);
			$this->load->view('index',$data);
			}
			else{
			/*
					$tgl= $this->input->post('tgl');
					$convert= strtotime($tgl);
					$day = 7;
					$tglawal = strtotime($tgl);
					$tgl=date("d m Y",$tglawal);
					$tgl_kembali=date("Y-m-d",strtotime("+".$day." day",$tglawal));
							
			date_default_timezone_set('UTC');
			$day = strtotime($tgl)-strtotime($aslitgl);
			$detik	=3600;
			$jam	=24;
			$day1= $day / $detik / $jam;
			echo $day1;				
			
		
				$id					= $this->uri->segment(3);
				$id_pinjam			= $this->uri->segment(3);
							$qr = $this->db->query("select m.nim, m.nama, m.id_member,b.id_buku, b.judul, p.tgl_pinjam, p.tgl_kembali
														from tb_pinjam p 
														join tb_member m ON m.id_member=p.id_member
														join tb_buku b ON b.id_buku=p.id_buku where p.id_pinjam=$id_pinjam and p.id_member=$id");
							$row = $qr->row();
			$data = array('id_buku' 	=>$this->input->post('id_buku'),
						'id_member'		=> $id,
						'tgl_pinjam'	=> $this->input->post('tgl'),
						'tgl_kembali'	=> $tgl_kembali,
						'denda'			=> $this->input->post('tgl'));
						
				$this->Peminjaman_model->addPeminjaman($data);
				*/
			$data1 = array('id_member'	=> $id,
						'tgl_pinjam'	=> $this->input->post('tgl'));
				$this->Peminjaman_model->addpinjam($data1);
				echo 'test';
				redirect('peminjaman');
						
			}
		
		}
		
		function simpan(){
			$tgl = $this->input->post('tgl');
			$tgl= $this->input->post('tgl');
					$convert= strtotime($tgl);
					$day = 7;
					$tglawal = strtotime($tgl);
					$tgl=date("d m Y",$tglawal);
					$tgl_kembali=date("Y-m-d",strtotime("+".$day." day",$tglawal));
					
			$id_member = $this->input->post('id_member');
			$data = array('id_member'	=> $id_member,
						'tgl_pinjam'	=> $this->input->post('tgl'),
						'tgl_hrs_kembali'	=> $tgl_kembali);
				$this->Peminjaman_model->addPeminjaman($data);
				
							
				$qr = $this->db->query("select MAX(id_peminjaman ) as id_peminjaman FROM tb_peminjaman");
				$res = $qr->row();
				
					
				$session=  $this->session->userdata('session_id');
				$this->db->where('session_id',$session);
				$query= $this->db->get('tb_ses')->result_array();
		
		foreach ($query as $row){
		$data = array('id_peminjaman' => $res->id_peminjaman,
					'id_member'		=>	$row['id_member'],
					'id_buku'		=> $row['id_buku']);
					
					$this->Peminjaman_model->addDetail($data);
		
		
		}
	//	$this->session->unset_userdata('session_id');
		$this->db->where('session_id',$session);
		$this->db->delete('tb_ses');
			
			
			
			redirect('peminjaman');
		
				
		}
		
		function edit(){
				$this->form_validation->set_rules('tgl','tanggal', 'required');
				$this->form_validation->set_rules('tgl_hrs_kembali','tanggal', 'required');
				$this->form_validation->set_rules('tgl_kembali','tanggal', 'required');
				if($this->form_validation->run() == FALSE){
				$data['title']		= 'Tambah Data Peminjaman';
				$id					= $this->uri->segment(3);
				$id_pinjam			= $this->uri->segment(3);
				$data['main_view']	= 'peminjaman/edit';
				$data['status'] = 'Edit Data Peminjaman';
				$data['row']			= $this->Peminjaman_model->getPeminjaman('by_id_member',$id,FALSE,FALSE);
				$this->load->view('index',$data);
				}
				else{
				$id					= $this->input->post('id_peminjaman');
				$id_pinjam			= $this->input->post('id_peminjaman');
				$data = array ('id_member'		=> $this->input->post('id_member'),
								'tgl_pinjam'	=> $this->input->post('tgl'),
								'tgl_hrs_kembali'	=> $this->input->post('tgl_hrs_kembali'),
								'tgl_kembali'	=> $this->input->post('tgl_kembali'),
								'denda'			=> $this->input->post('denda'));
							$this->Peminjaman_model->editPeminjaman($id,$data);
				$id_detail = $this->input->post('id_detail');
				$data = array ('id_peminjaman'	=> $id_pinjam,
								'id_member'		=> $this->input->post('id_member'),
								'id_buku'		=> $this->input->post('id_buku'));
				
							$this->Peminjaman_model->editDetail($id_detail,$data);
					$id_member = $this->uri->segment(4);
					redirect('peminjaman/detail_peminjaman/'.$id_pinjam.'/'.$id_member);
			
			}
		
		}
	

		function detail_peminjaman(){
				$this->form_validation->set_rules('id_peminjaman', 'id_peminjaman', 'required');
				if ($this->form_validation->run() == FALSE){
						
						$id_member = $this->uri->segment(4);
						$id_peminjaman = $this->uri->segment(3);
				$qr = $this->db->query("select id_member, tgl_kembali, tgl_pinjam 
										from tb_peminjaman 
										where id_member=$id_member");
				$rows = $qr->row();
				$datestring = "%Y-%m-%d";
				$tgl_sekarang= mdate($datestring);	
									
				$tgl_tagihan = $rows->tgl_kembali;	
					
					
				$tgl_pinjam= $rows->tgl_pinjam;
					$convert= strtotime($tgl_sekarang);
					$day = 7 ;
					$tglawal = strtotime($tgl_sekarang);
					$tgl=date("d m Y",$tglawal);
					$tgl_kembali=date("Y-m-d",strtotime("+".$day." day",$tglawal));
						
					
					//echo $tgl_tagihan;
							date_default_timezone_set('UTC');
							$day =strtotime($tgl_sekarang) - strtotime($tgl_pinjam);
							$detik	=3600;
							$jam	=24;
							$day1= $day / $detik / $jam;
						//	echo $day1;
							if ($day1 >= 7){
							
											$denda = $day1 - 7;
											$hasih = $denda * 1000;
											$data['tagihan'] =  $hasih;
											$data['hari']	= $denda;
											}
							else {
							$data['tagihan'] = '0';
							$data['hari']	= '0';
							}
							
			$id = $this->uri->segment(3);
			$data['title']	= 'Detail Peminjaman';
			$data['main_view']		= 'peminjaman/detail';
			$data['status'] = 'Detail Data Peminjaman';
			$data['row']			= $this->Peminjaman_model->getPeminjaman('by_id',$id,FALSE,FALSE);
			$data['query']			= $this->Peminjaman_model->getPeminjaman('by_judul',$id,FALSE,FALSE);
			$this->load->view('index',$data);
				}
				
				else{
				$datestring = "%Y-%m-%d";
				$tgl_sekarang= mdate($datestring);	
				$id_member = $this->input->post('id_member');
				$id = $this->input->post('id_peminjaman');
			$status	= 0;
			$data = array ('tgl_kembali'	=> $tgl_sekarang,
							'denda'			=> $this->input->post('denda'),
							'status' => $status);
			$this->Peminjaman_model->EditPeminjaman($id,$data);
					redirect('peminjaman');
				}
		
		
		
		
		
		
		}
	
		function insert_temp_pinjam(){
		$this->form_validation->set_rules('id_buku','id_buku', 'required');
		
			if($this->form_validation->run() == FALSE ){
			
			$data['title']		= 'Tambah Data Peminjaman';
				$id					= $this->uri->segment(4);
				$id_pinjam			= $this->uri->segment(3);
				$data['main_view']	= 'peminjaman/tambah';
			
			}
			else{
			$id_member = $this->input->post('id_member');
			$data = array ('session_id' => $this->session->userdata('session_id'),
						'id_member'		=> $this->input->post('id_member'),
						'id_buku'		=> $this->input->post('id_buku'),
						'tgl_pinjam'		=> $this->input->post('tgl'));
						
						$this->Peminjaman_model->addTempPinjam($data);
						redirect('peminjaman/tambah/'.$id_member);
			}
		
		
		}
				
		
		
			
			function delete(){
				$id_pinjam = $this->uri->segment(4);
				$id = $this->uri->segment(3);
				$this->Peminjaman_model->deletePeminjaman($id);
				redirect('peminjaman/detail_peminjaman/'.$id_pinjam);
			
			}
}



